package com.onccc.social.step

import android.view.accessibility.AccessibilityNodeInfo
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.ScreenUtils
import com.onccc.social.Assists
import com.onccc.social.Assists.click
import com.onccc.social.Assists.getChildren
import com.onccc.social.Assists.log
import com.onccc.social.Assists.paste
import com.onccc.social.AssistsWindowManager
import com.onccc.social.MainActivity
import com.onccc.social.MainActivity.Companion.getComment
import com.onccc.social.MainActivity.Companion.getInputTime
import com.onccc.social.MainActivity.Companion.getInterest
import com.onccc.social.MainActivity.Companion.getOneTime
import com.onccc.social.MainActivity.Companion.getTwoTime
import com.onccc.social.OverManager
import com.onccc.social.model.CommentModel
import com.onccc.social.model.ImageInterestModel
import com.onccc.social.stepper.Step
import com.onccc.social.stepper.StepCollector
import com.onccc.social.stepper.StepImpl
import com.onccc.social.stepper.StepManager
import kotlinx.coroutines.delay
import java.util.Random


/**
 * @author GongChengHao
 * @date 2024/12/4
 * Description :
 */
class PinterestSocial : StepImpl() {

    var mPackageName = "com.pinterest"

    private var maxSelect = 4
    private var selectIndex = 0
    private var mCommentModel = CommentModel()
    private var mImageInterestModel = ImageInterestModel()
    private var videoIndex = 0

    private var getShort = 0

    override fun onImpl(collector: StepCollector) {
        collector.next(StepTag.STEP_1) {
            OverManager.log("启动Pinterest")

            AppUtils.launchApp(mPackageName)
            runIO { delay(getOneTime()) }

            Assists.findByText("Accept").forEach() {
                runIO { delay(1000) }
                it.click()
            }

            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_2) {
            runIO { delay(getTwoTime()) }

            Assists.findById("com.pinterest:id/menu_search").firstOrNull() {
                OverManager.log("点击搜索")
                it.click()
                runIO { delay(getTwoTime()) }
                return@next Step.get(StepTag.STEP_3)
            }
            if (Assists.getPackageName() == mPackageName) {
                runIO { delay(getOneTime()) }
                Assists.back()
                return@next Step.get(StepTag.STEP_2)
            }
            return@next Step.get(StepTag.STEP_1)
        }.next(StepTag.STEP_3) {
            runIO { delay(getTwoTime()) }
            OverManager.log("输入搜索内容")
            Assists.findById("com.pinterest:id/search_tv").firstOrNull() {
                Assists.gestureViewClick(it)
                return@next Step.get(StepTag.STEP_4)
            }

            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_4) {
            runIO { delay(getTwoTime()) }
            Assists.findByTags("android.widget.EditText").forEach() {
                it.performAction(AccessibilityNodeInfo.ACTION_FOCUS)
                runIO { delay(getOneTime()) }
                for (s in MainActivity.getKeyword()){
                    it.paste(s.toString())
                    runIO { delay(getInputTime()) }
                }
                runIO { delay(getTwoTime()) }

                Assists.gestureKeyboardClick()
                return@next Step.get(StepTag.STEP_100)
            }

            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_21) {
            runIO { delay(getTwoTime()) }
            OverManager.log("按右上角按钮")
            Assists.findById("com.pinterest:id/bar_icons").forEach() {
                runIO { delay(getTwoTime()) }
                Assists.gestureViewClick(it.getChild(0))
                Assists.findById("com.pinterest:id/product_filter_recycler_view").forEach() { it1 ->
                    runIO { delay(getTwoTime()) }
                    OverManager.log("寻找vide按钮")
                    val ass = it1.getChild(0).getChild(1)
                    ass.log()
                    OverManager.log("判断video按钮")
                    if (!ass.getChild(1).isChecked) {
                        runIO { delay(getOneTime()) }
                        Assists.gestureViewClick(ass)
                        Assists.findById("com.pinterest:id/confirm_button").forEach() { it2 ->
                            OverManager.log("可以按")
                            runIO { delay(getOneTime()) }
                            Assists.gestureViewClick(it2)
                        }
                    } else {
                        Assists.findById("com.pinterest:id/reset_button").forEach() { it3 ->
                            OverManager.log("不能按")
                            runIO { delay(getOneTime()) }
                            Assists.gestureViewClick(it3)
                        }
                    }
                    return@next Step.get(StepTag.STEP_5)
                }


            }
            return@next Step.get(StepTag.STEP_5)
        }.next(StepTag.STEP_5) {

            selectIndex++
            if (selectIndex > maxSelect) {
                return@next Step.get(StepTag.STEP_100)
            }

            if (Random().nextInt(10) % 2 == 0) {
                OverManager.log("随机直接点")
                return@next Step.get(StepTag.STEP_6)
            }

            runIO { delay(getOneTime()) }
            OverManager.log("随机选择向下滑动")
            AssistsWindowManager.switchNotTouchableAll()
            val xL = ScreenUtils.getAppScreenWidth() / 4F + Random().nextInt(500).toFloat()
            val xR = ScreenUtils.getAppScreenWidth() / 4F + Random().nextInt(500).toFloat()
            val distance = ScreenUtils.getAppScreenHeight() / 5F + Random().nextInt(300).toFloat()
            val startY = ScreenUtils.getAppScreenHeight() - distance
            val endY = ScreenUtils.getAppScreenHeight() - Random().nextInt(800) - distance
            OverManager.log("滑动：$xL/$startY,$xR/$endY")
            Assists.gesture(
                    floatArrayOf(xL, startY), floatArrayOf(xR, endY), 0, 460L + Random().nextInt(300).toLong()
            )
            AssistsWindowManager.switchTouchableAll()

            return@next Step.get(StepTag.STEP_6)


        }.next(StepTag.STEP_6) {
            runIO { delay(getOneTime()) }

            val x = 100 + Random().nextInt(ScreenUtils.getAppScreenWidth() - 100).toFloat()
            val y = 300 + Random().nextInt(ScreenUtils.getAppScreenWidth() - 300).toFloat()
            val clickTime = 54L + Random().nextInt(40)
            OverManager.log("点击：$x,$y,时间$clickTime")
            runIO { delay(getOneTime()) }
            Assists.gestureClick(x, y, clickTime)
            runIO { delay(getTwoTime()) }
            return@next Step.get(StepTag.STEP_7)
        }.next(StepTag.STEP_7) {
            getInterest({ interestModel ->
                mImageInterestModel = interestModel
                StepManager.execute(PinterestSocial::class.java, StepTag.STEP_8, begin = true)
            })
            {
                StepManager.execute(PinterestSocial::class.java, StepTag.STEP_5, begin = true)

            }
            return@next Step.none
        }.next(StepTag.STEP_8) {
            var time = 0
            if (mImageInterestModel.isToLike) {
                time = 3000 + kotlin.random.Random.nextInt(5000)
            }
            if (mImageInterestModel.isToFollow) {
                time += kotlin.random.Random.nextInt(5000)
            }
            if (mImageInterestModel.isToComment) {
                time += kotlin.random.Random.nextInt(5000)
            }
            runIO { delay(time.toLong()) }

            if (mImageInterestModel.isToLike) {
                Assists.findById("com.pinterest:id/action_module_react_icon_sab").forEach {

                    Assists.gestureViewClick(it)
                    return@next Step.get(StepTag.STEP_9)
                }
            }
            return@next Step.get(StepTag.STEP_9)

        }.next(StepTag.STEP_9) {
            if (mImageInterestModel.isToFollow) {
                runIO { delay(getOneTime()) }
                Assists.findById("com.twitter.android:id/tweet_curation_action").forEach() { it ->
                    Assists.gestureViewClick(it)
                    runIO { delay(getOneTime()) }
                    Assists.findById("com.twitter.android:id/action_sheet_recycler_view").firstOrNull { it1 ->
                        val btnFollow = it1.getChildren()[0]
                        Assists.gestureViewClick(btnFollow)
                        runIO { delay(getOneTime()) }
                        return@next Step.get(StepTag.STEP_10)
                    }
                }
            }
            return@next Step.get(StepTag.STEP_10)
        }.next(StepTag.STEP_10) {
            if (mImageInterestModel.isToComment) {
                Assists.findById("com.pinterest:id/action_module_comment_count").forEach() { it1 ->
                    return@next Step.get(StepTag.STEP_11)
                }
            }
            return@next Step.get(StepTag.STEP_5)
        }.next(StepTag.STEP_11) {
            runIO { delay(getOneTime()) }
            OverManager.log("查找评论按钮")
            Assists.findById("com.pinterest:id/action_module_comments_icon").forEach {
                runIO { delay(getOneTime()) }
                Assists.gestureViewClick(it)
                return@next Step.get(StepTag.STEP_12)
            }
            runIO { delay(getOneTime()) }

            Assists.back()
            return@next Step.get(StepTag.STEP_5)
        }.next(StepTag.STEP_12) {

            getComment({ commentModel ->
                mCommentModel = commentModel
                StepManager.execute(PinterestSocial::class.java, StepTag.STEP_13, begin = true)
            })
            {
                StepManager.execute(PinterestSocial::class.java, StepTag.STEP_15, begin = true)
            }
            return@next Step.none
        }.next(StepTag.STEP_13) {
            runIO { delay(getOneTime()) }
            OverManager.log("发送评论")
            Assists.findById("com.pinterest:id/edit_text").forEach {
                Assists.gestureViewClick(it)
                runIO { delay(getTwoTime()) }

                Assists.findById("com.pinterest:id/text_edit").forEach { itEdit ->

                    runIO { delay(getOneTime()) }
                    for (s in mCommentModel.comment) {
                        itEdit.paste(s.toString())
                        runIO { delay(getInputTime()) }
                    }

                }

                return@next Step.get(StepTag.STEP_14)
            }
            return@next Step.get(StepTag.STEP_15)
        }.next(StepTag.STEP_14) {
            OverManager.log("点击发送")
            runIO { delay(getOneTime()) }

            Assists.findById("com.pinterest:id/post_button").forEach {
                runIO { delay(getOneTime()) }
                Assists.gestureViewClick(it)
                return@next Step.get(StepTag.STEP_15)
            }
            return@next Step.none
        }.next(StepTag.STEP_15) {
            runIO { delay(getOneTime()) }
            Assists.back()
            runIO { delay(getTwoTime()) }
            Assists.back()
            return@next Step.get(StepTag.STEP_5)
        }.next(StepTag.STEP_100) {
            OverManager.log("结束")
            return@next Step.none
        }
    }
}